package com.example.clock_demo.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * 月度考勤汇总Excel处理器 - 最终纯净版
 */
public class MonthlyAttendanceSummaryExcelHandler implements SheetWriteHandler {


    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = writeSheetHolder.getSheet();

        // 创建无背景色的样式
        CellStyle headerStyle = createSimpleHeaderStyle(workbook);
        CellStyle subHeaderStyle = createSimpleSubHeaderStyle(workbook);

        // 直接创建数据表头
        createDataHeaders(sheet, headerStyle, subHeaderStyle);
    }

    /**
     * 只创建数据表头，从第0行开始
     */
    private void createDataHeaders(Sheet sheet, CellStyle headerStyle, CellStyle subHeaderStyle) {

        // 第0行：主表头
        Row headerRow1 = sheet.createRow(0);
        String[] headers1 = {"序号", "班组名称", "总人数", "缺勤人数", "", "", "白班天数", "夜班天数"};
        for (int i = 0; i < headers1.length; i++) {
            Cell cell = headerRow1.createCell(i);
            cell.setCellValue(headers1[i]);
            cell.setCellStyle(headerStyle);
        }

        // 第1行：子表头
        Row headerRow2 = sheet.createRow(1);
        String[] headers2 = {"", "", "", ">=10天", ">=5天且<10天", ">=1天且<5天", "", ""};
        for (int i = 0; i < headers2.length; i++) {
            Cell cell = headerRow2.createCell(i);
            cell.setCellValue(headers2[i]);
            cell.setCellStyle(subHeaderStyle);
        }

        // 合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0)); // 序号
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1)); // 班组名称
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2)); // 总人数
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 5)); // 缺勤人数
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 6, 6)); // 白班天数
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 7, 7)); // 夜班天数

        // 设置行高
        headerRow1.setHeightInPoints(25);
        headerRow2.setHeightInPoints(25);

        // 设置列宽
        setColumnWidths(sheet);
    }

    private void setColumnWidths(Sheet sheet) {
        sheet.setColumnWidth(0, 8 * 256);   // 序号
        sheet.setColumnWidth(1, 20 * 256);  // 班组名称
        sheet.setColumnWidth(2, 12 * 256);  // 总人数
        sheet.setColumnWidth(3, 12 * 256);  // >=10天
        sheet.setColumnWidth(4, 18 * 256);  // >=5天且<10天
        sheet.setColumnWidth(5, 18 * 256);  // >=1天且<5天
        sheet.setColumnWidth(6, 12 * 256);  // 白班天数
        sheet.setColumnWidth(7, 12 * 256);  // 夜班天数
    }

    // 样式方法（无背景色，只有边框和字体样式）
    private CellStyle createSimpleHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 11);
        style.setFont(font);

        return style;
    }

    private CellStyle createSimpleSubHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 10);
        style.setFont(font);

        return style;
    }
}
